package com.example.back.controller;

import com.example.back.entity.Comment;
import com.example.back.entity.otd.CommentDTO;
import com.example.back.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class CommentController {
    @Autowired
    private CommentService commentService;

    // 回复评论（通过parentId区分父子）
    @PostMapping("/comment")
    public int addComment(@RequestBody Comment comment) {
        return commentService.addComment(comment);
    }
    @DeleteMapping("/comment/{id}")
    public int deleteComment(@PathVariable("id") Integer id){
        return commentService.deleteComment(id);
    }
    @PutMapping("/comment")
    public int updateComment(@RequestBody Comment comment){
        return commentService.updateComment(comment);
    }
    @GetMapping("/comment/{id}")
    public Comment queryCommentById(@PathVariable("id") Integer id){
        return commentService.queryCommentById(id);
    }
    @GetMapping("/comment")
    public ArrayList<Comment> queryComments(@RequestBody Comment comment){
        return commentService.queryComments(comment);
    }

    //==================== 查询指定文章的评论（带评论者信息）====================
    @GetMapping("/commentp/{postId}")
    public ArrayList<CommentDTO> queryCommentsByPostId(@PathVariable("postId") Integer postId){
        return commentService.queryCommentsByPostId(postId);
    }





    // 查询帖子的所有评论（包含嵌套回复）
    @GetMapping("/commentp/withReplies/{postId}")
    public List<CommentDTO> queryCommentsWithReplies(@PathVariable Integer postId) {
        return commentService.queryCommentsWithReplies(postId);
    }
}
